package com.qfedu.ssm.handler;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;

@Component
public class ZanHandler {

    @Autowired
    private StringRedisTemplate redisTemplate;

    public void addZan(String itemId,int userId,String type) throws Exception{
        String key = "wzc-redis-zan" + itemId + "-" + userId;
        if ("false".equals(type)) {
            redisTemplate.boundValueOps(key).set("1");
        }else {
            redisTemplate.boundValueOps(key).set("0");
        }
    }

    public long countZan(String itemId,String type){
        long num = 1;
        String key = "wzc-redis-zan"+itemId;
        if ("false".equals(type)) {
            if (redisTemplate.hasKey(key)) {
                num = redisTemplate.boundValueOps(key).increment(1);
            } else {
                redisTemplate.boundValueOps(key).set("1");
            }
        }else {
                num = redisTemplate.boundValueOps(key).increment(-1);
        }
        return num;
    }


}
